Learning Seaborn

Seaborn is a great python library for data viz, but I don't know it well. Here I follow the tutorial provided on the seaborn docs website:


In [1]:
%matplotlib inline

In [28]:
import numpy as np
import matplotlib as mpl
from matplotlib import pyplot as plt
import time

In [35]:
# np.random.seed(sum(map(ord, "aesthetics")))
rng = np.random.RandomState(seed=1069) # same as the code above, which a pretentious way of setting seed = 1069

First we make a plot example.


In [12]:
def sinplot(flip=1):
    x = np.linspace(0, 14, 100)
    for i in range(1, 7):
        plt.plot(x, np.sin(x + i * .5) * (7 - i) * flip)

When we call it, matplotlib plots things with its defaults. Note that seaborn hasn't been import yet in any code thus far.


In [20]:
sinplot(1)


To get seaborn styling, merely import the package:


In [21]:
import seaborn as sns

In [23]:
sinplot()


from the docs:

  • "There are five preset seaborn themes: darkgrid, whitegrid, dark, white, and ticks"

In [32]:
for style in ('darkgrid', 'whitegrid', 'dark', 'white', 'ticks'):
    sns.set_style(style)
    sinplot()
    plt.show()



In [60]:
rng = np.random.RandomState(1)
data = rng.normal(size=(20, 6)) + np.arange(6) / 2
sns.set_style('whitegrid')
sns.boxplot(data)


Out[60]:
<matplotlib.axes._subplots.AxesSubplot at 0x7f8ec3c1e090>

We can also use despine() to remove axes bars on the bottom, left, right and/or top of the plot area. This call must come after the plots have been generated.


In [64]:
sns.set_style('darkgrid')
sinplot()
sns.despine(right = False, left = True)